package com.hyl.message.service;

import cn.hutool.extra.mail.MailUtil;
import com.hyl.message.constants.MQConstant;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author leaf
 * @description TODO
 * @created 2023/7/20
 */
@Service
public class ConsumerService {

    /**
     * 监听消息并处理
     * @param message
     */
    @RabbitListener(queues = {MQConstant.QUEUE_NAME})
    public void handlerQueue(Message message){
        System.out.println(new String(message.getBody()));
    }
    /**
     * 简单模式进行监听处理消息
     * @param message
     */
    @RabbitListener(queues = {MQConstant.QUEUE_SIMPLE_NAME})
    public void handlerSimpleQueue(Message message){
        System.out.println(new String(message.getBody()));
    }

    /**
     * 工作队列监听：处理
     * @param message
     */
    @RabbitListener(queues = {MQConstant.QUEUE_WORD_NAME})
    public void handlerWorkQueue(Message message, Channel channel){
        System.out.println(new String(message.getBody()));
        try {
            Thread.sleep(2000);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 订阅分发队列监听：处理
     * @param message
     */
    @RabbitListener(queues = {MQConstant.QUEUE_SUB_PRO})
    public void handlerSubProQueue(Message message){
        System.out.println("pro"+new String(message.getBody()));
    }


    /**
     * 订阅分发队列监听：处理
     * @param message
     */
    @RabbitListener(queues = {MQConstant.QUEUE_SUB_VUE,MQConstant.QUEUE_SIMPLE_NAME})
    public void handlerSubVUEQueue(Message message) {
        System.out.println("VUE"+new String(message.getBody()));

    }
}
